package cn.team11.smarteye.fragment;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import cn.team11.sds.dayi.R;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.team11.smarteye.base.BaseFragment;
import cn.team11.smarteye.base.BasePresenter;
import cn.team11.smarteye.utils.ToastUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by Yang on 2017/10/28.
 */

public class DeviceBindingFragment extends BaseFragment{

    @BindView(R.id.connecting_img)
    ImageView mImageView;

    private AnimationDrawable mAnimation;
    private Disposable mDisposable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view_device_connecting, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    public static DeviceBindingFragment newInstance(Bundle bundle) {
        DeviceBindingFragment fragment = new DeviceBindingFragment();
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    public void init() {
        mAnimation  = (AnimationDrawable) mImageView.getDrawable();
        mAnimation.start();
        mDisposable = createTimer()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                mAnimation.stop();
                ToastUtil.showNegativeToast(getString(R.string.binding_failed));
                getActivity().onBackPressed();
            }
        });
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (!mDisposable.isDisposed()){
            mDisposable.dispose();
        }
    }

    @Override
    public void setPresenter(BasePresenter presenter) {

    }


    private Observable createTimer(){
        return Observable.timer(120, TimeUnit.SECONDS);
    }


}
